package org.batatasframework.support.database.parameters;

import java.sql.PreparedStatement;

public class DecimalParamenter extends AbstractQueryParameter<Double>{
	
	public DecimalParamenter(int index, Double value){
		super(index, value);
	}
	
	public static final Double valueOf(Object value){
		if(value.toString().isEmpty()) return null;
		if(value instanceof Number){
			return ((Number) value).doubleValue();
		}else if(value instanceof Boolean){
			return ((Boolean)value).booleanValue() ? 1D : 0D;
		}else{
			return Double.parseDouble(value.toString());
		}
	}

	public DecimalParamenter(int index, Object value){
		super(index, valueOf(value));
	}

	public DecimalParamenter(int index, String value){
		super(index, Double.parseDouble(value));
	}
	
	@Override
	public void set(PreparedStatement statement) throws Exception {
		statement.setDouble(index, value);
	}

}
